#pragma once
#include "Protocol.hpp"
#include "Log.hpp"
using namespace LogModule;

class Calculator
{
public:
    Response Calculate(const Request &request)
    {
        int result = 0, status = 0;
        int x = request.X(), y = request.Y();
        char oper = request.Oper();
        switch (oper)
        {
        case '+':
            result = x + y;
            break;
        case '-':
            result = x - y;
            break;
        case '*':
            result = x * y;
            break;
        case '/':
            if(y == 0)
            {
                LOG(LogLevel::ERROR) << "Calculator: 除零错误! ";
                status = 1;
            }
            else
                result = x / y;
            break;
        case '%':
            if(y == 0)
            {
                LOG(LogLevel::ERROR) << "Calculator: 模零错误! ";
                status = 2;
            }
            else
                result = x % y;
            break;
        case '^':
            result = x ^ y;
            break;
        case '&':
            result = x & y;
            break;
        case '|':
            result = x | y;
            break;
        default:
            LOG(LogLevel::ERROR) << "Calculator: 未知类型的运算符[" << oper << "]";
            status = 2;
            break;
        }
        Response response(result, status);
        return response;
    }
};